package com.nageoffer.shortlink.project.annotation;

import cn.hutool.core.bean.BeanUtil;
import com.nageoffer.shortlink.project.dao.mapper.LinkGroupMapper;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;


import java.rmi.ServerException;

import static com.nageoffer.shortlink.project.toolkit.LinkUtil.checkGroupBelongToUser;

@Aspect
@Component
@RequiredArgsConstructor
public class RequiresPermissionAspect {
    private final LinkGroupMapper linkGroupMapper;
    @Around("@annotation(RequiresPermission)")
    public Object checkPermission(ProceedingJoinPoint joinPoint) throws Throwable {
        if(joinPoint.getArgs().length == 0){
            throw new ServerException("未传入正确参数");
        }
        String gid= (String) BeanUtil.beanToMap(joinPoint.getArgs()[0]).get("gid");
        if(gid==null){
            throw new ServerException("未传入正确参数");
        }
        checkGroupBelongToUser(gid,linkGroupMapper);
        return joinPoint.proceed();
    }
}
